/*
 * PGN_Reader : A simple pgn reader for chess games.
 * Copyright (C) 2011 Laurent BERNABE
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.gmail.bernabe.laurent.java_swing.pgn_reader.processing.piece;

public class Piece {
	
	public PieceForce force;
	public Player owner;
	
	public Piece(PieceForce force, Player owner) {
		this.force = force;
		this.owner = owner;
	}
	
	/**
	 * Alternative constructor, this time you give an int
	 * for playerValue : either 0 (white) or 1 (black)
	 * @param force - PieceForce - the force
	 * @param playerValue - int - 0 (white) or 1(black)
	 */
	public Piece(PieceForce force, int playerValue){
		assert (playerValue >= 0);
		assert (playerValue < 2);
		
		this.force = force;
		if (playerValue == 0)
			owner = Player.white;
		else
			owner = Player.black;
	}
	
	public boolean equals(Object other){
		boolean stateToReturn;
		if (other != null) {
			if (other instanceof Piece) {
				Piece otherPiece = (Piece) other;
				stateToReturn = (otherPiece.force.toString().equals(force.toString()))
						&& (otherPiece.owner == owner);
			}
			else
				stateToReturn = false;
		}
		else
			stateToReturn = false;
		return stateToReturn;
	}
	
	public int hashCode(){
		int forceHashCode = force.hashCode();
		int ownerHashCode = owner.hashCode();
		return forceHashCode * ownerHashCode + forceHashCode + ownerHashCode;
	}
	
	/**
	 *  Builds a Piece from a FEN initial : FEN is common chess
	 *  format in order to represent the chess board in a given state.
	 *  For example, a FEN line is 3pR3, which means a line made of 3 empty spaces,
	 *  the a black pawn (as it is in lower case), a white Rook (as it is in uppercase) and 3 empty spaces.
	 *  And from left to right.
	 *  @param piece_FEN_initial - char - either {p,n,b,r,q,k,P,N,B,R,Q or K}
	 */
	public Piece(char piece_FEN_initial){
		switch(piece_FEN_initial){
		case 'p':
			force = PieceForce.pawn;
			owner = Player.black;
			break;
		case 'n':
			force = PieceForce.knight;
			owner = Player.black;
			break;
		case 'b':
			force = PieceForce.bishop;
			owner = Player.black;
			break;
		case 'r':
			force = PieceForce.rook;
			owner = Player.black;
			break;
		case 'q':
			force = PieceForce.queen;
			owner = Player.black;
			break;
		case 'k':
			force = PieceForce.king;
			owner = Player.black;
			break;
		case 'P':
			force = PieceForce.pawn;
			owner = Player.white;
			break;
		case 'N':
			force = PieceForce.knight;
			owner = Player.white;
			break;
		case 'B':
			force = PieceForce.bishop;
			owner = Player.white;
			break;
		case 'R':
			force = PieceForce.rook;
			owner = Player.white;
			break;
		case 'Q':
			force = PieceForce.queen;
			owner = Player.white;
			break;
		case 'K':
			force = PieceForce.king;
			owner = Player.white;
			break;
		}
	}
	
	public String toString(){
		StringBuilder strToReturn = new StringBuilder("Piece : [ force : ");
		strToReturn.append(force.toString());
		strToReturn.append(", owner : ");
		strToReturn.append(owner);
		strToReturn.append(" ]");
		return strToReturn.toString();
	}
	
	public boolean isALongRangedPiece(){
		return force.isALongRangedPieceForce();
	}
	
}
